package com.lb.jwitter.action;


import net.sf.json.JSONArray; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import com.lb.jwitter.util.ApplicationContextProvider; 
import net.sf.json.JSONObject; 
import java.util.Map; 
import com.opensymphony.xwork2.ActionContext; 
import net.sf.json.JSONSerializer; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.Date; 
import java.util.List; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHoc;
import com.lb.jwitter.service.TblQLDTTXAHocVienPhieuDangKyMonHocService;
import com.lb.jwitter.service.TblQLDTTXAHocVien;
import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHoc;

import com.lb.jwitter.service.TblQLDTTXAHocVienDangKyMonHocService;

import com.lb.jwitter.service.TblQLDTTXAMonHocService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienPhieuDangKyMonHocUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private int hocVienId; 
    public void setHocVienId(int hocVienId) {
       this.hocVienId = hocVienId;
 }

    public int getHocVienId() {
       return hocVienId;
 }

    private String ngayNop; 
    public void setNgayNop(String ngayNop) {
       this.ngayNop = ngayNop;
 }

    public String getNgayNop() {
       return ngayNop;
 }

    private int canBoNhanId; 
    public void setCanBoNhanId(int canBoNhanId) {
       this.canBoNhanId = canBoNhanId;
 }

    public int getCanBoNhanId() {
       return canBoNhanId;
 }

    private int hocKyId; 
    public void setHocKyId(int hocKyId) {
       this.hocKyId = hocKyId;
 }

    public int getHocKyId() {
       return hocKyId;
 }

    private TblQLDTTXAHocVienPhieuDangKyMonHoc tblQLDTTXAHocVienPhieuDangKyMonHoc; 
    public void setTblQLDTTXAHocVienPhieuDangKyMonHoc (TblQLDTTXAHocVienPhieuDangKyMonHoc tblQLDTTXAHocVienPhieuDangKyMonHoc) {
        this.tblQLDTTXAHocVienPhieuDangKyMonHoc = tblQLDTTXAHocVienPhieuDangKyMonHoc;
 }

    private Collection<TblQLDTTXAHocVienPhieuDangKyMonHoc> tblQLDTTXAHocVienPhieuDangKyMonHocs; 
    public void setTblQLDTTXAHocVienPhieuDangKyMonHocs(Collection<TblQLDTTXAHocVienPhieuDangKyMonHoc> tblQLDTTXAHocVienPhieuDangKyMonHocs) {
        this.tblQLDTTXAHocVienPhieuDangKyMonHocs = tblQLDTTXAHocVienPhieuDangKyMonHocs; 
      } 

    private TblQLDTTXAHocVienPhieuDangKyMonHocService tblQLDTTXAHocVienPhieuDangKyMonHocService; 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private TblCanBoService tblCanBoService; 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 

    private String tblQLDTTXAHocVienDangKyMonHocStr; 
    public void setTblQLDTTXAHocVienDangKyMonHocStr (String tblQLDTTXAHocVienDangKyMonHocStr) 
 {     this.tblQLDTTXAHocVienDangKyMonHocStr = tblQLDTTXAHocVienDangKyMonHocStr;
 }

    public String getTblQLDTTXAHocVienDangKyMonHocStr() {
       return tblQLDTTXAHocVienDangKyMonHocStr;
 }

    private TblQLDTTXAHocVienDangKyMonHocService tblQLDTTXAHocVienDangKyMonHocService; 

    private TblQLDTTXAMonHocService tblQLDTTXAMonHocService; 

	private int preUpdate(Message msg, TblQLDTTXAHocVienPhieuDangKyMonHoc obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblQLDTTXAHocVienPhieuDangKyMonHoc obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String execute() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc", "sua")){ 
        return "noprivilege"; 
    }        tblQLDTTXAHocVienPhieuDangKyMonHocService = (TblQLDTTXAHocVienPhieuDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_PhieuDangKyMonHocClient"); 
        tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXAHocVienDangKyMonHocService = (TblQLDTTXAHocVienDangKyMonHocService) context.getBean("tbl_QLDTTXA_HocVien_DangKyMonHocClient"); 

        tblQLDTTXAMonHocService = (TblQLDTTXAMonHocService) context.getBean("tbl_QLDTTXA_MonHocClient"); 

        msg = new Message(); 
        tblQLDTTXAHocVienPhieuDangKyMonHoc = tblQLDTTXAHocVienPhieuDangKyMonHocService.gettblQLDTTXAHocVienPhieuDangKyMonHoc(new Long(id)); 
        tblQLDTTXAHocVienPhieuDangKyMonHoc.setHocVienId(tblQLDTTXAHocVienService.gettblQLDTTXAHocVien(new Long(hocVienId))); 
       DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
       Date dd = df.parse(ngayNop); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblQLDTTXAHocVienPhieuDangKyMonHoc.setNgayNop(d); 
        tblQLDTTXAHocVienPhieuDangKyMonHoc.setCanBoNhanId(tblCanBoService.gettblCanBo(new Long(canBoNhanId))); 
        //tblQLDTTXAHocVienPhieuDangKyMonHoc.setHocKyId(tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(new Long(hocKyId))); 
        preUpdate(msg,tblQLDTTXAHocVienPhieuDangKyMonHoc); 
       if (msg.isPreMessage_b()) 
       { 
           tblQLDTTXAHocVienPhieuDangKyMonHocService.updatetblQLDTTXAHocVienPhieuDangKyMonHoc(tblQLDTTXAHocVienPhieuDangKyMonHoc); 
           postUpdate(msg, tblQLDTTXAHocVienPhieuDangKyMonHoc); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

    List < ConditionReport > cons = new ArrayList<ConditionReport>(); 
    {
	    ConditionReport con = new ConditionReport(); 
	    con.setCol("phieuDangKyId.id"); 
	    con.setValue1(tblQLDTTXAHocVienPhieuDangKyMonHoc.getId()); 
	    con.setOperator("eq"); 
	    cons.add(con);
    }
    {
	    ConditionReport con = new ConditionReport(); 
	    con.setCol("daXepLop"); 
	    con.setValue1(false); 
	    con.setOperator("eq"); 
	    cons.add(con);
    }
    List<TblQLDTTXAHocVienDangKyMonHoc> tblQLDTTXAHocVienDangKyMonHocList = tblQLDTTXAHocVienDangKyMonHocService.findtblQLDTTXAHocVienDangKyMonHocByCondition(cons, 0, 0); 
    if(tblQLDTTXAHocVienDangKyMonHocList != null && tblQLDTTXAHocVienDangKyMonHocList.size() >0){ 
         List<String> ids = new ArrayList<String>();  
         for(TblQLDTTXAHocVienDangKyMonHoc obj0 : tblQLDTTXAHocVienDangKyMonHocList ){ 
             Collections.addAll(ids, obj0.getId().toString());  
         } 
         tblQLDTTXAHocVienDangKyMonHocService.deleteListtblQLDTTXAHocVienDangKyMonHoc(ids);  
     }
    JSONArray jsonTblQLDTTXAHocVienDangKyMonHoc = (JSONArray)JSONSerializer.toJSON(tblQLDTTXAHocVienDangKyMonHocStr); 
    for (int i = 0; i < jsonTblQLDTTXAHocVienDangKyMonHoc.size(); i++) 
     { 
         JSONObject jsObj = jsonTblQLDTTXAHocVienDangKyMonHoc.getJSONObject(i); 
         TblQLDTTXAHocVienDangKyMonHoc obj0 = new TblQLDTTXAHocVienDangKyMonHoc(); 
        obj0.setMonHocId(tblQLDTTXAMonHocService.gettblQLDTTXAMonHoc(new Long(jsObj.getInt("monHocId")))); 
       obj0.setPhieuDangKyId(tblQLDTTXAHocVienPhieuDangKyMonHoc);
       tblQLDTTXAHocVienDangKyMonHocService.createtblQLDTTXAHocVienDangKyMonHoc(obj0); 
   } 
        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_QLDTTXA_HocVien_PhieuDangKyMonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
